define([
    'require',
    'angular',
    'underscore'
], function (require, angular, _) {
    return  function ($scope,Resource,$location,GillionMsg,$dataSourceManager,$rootScope,RuleSerivce,$timeout,$http) {
        //var variables = Resource("/grule-manager/variables/"+$scope.scope+"/"+$scope.scopeId+"/:id", {id: "@id"});
        RuleSerivce.promises().then(function () {
            $scope.scope=$rootScope.variableScope;
            if($scope.scope==1){
                $scope.scopeId=$rootScope.currPackage.id;
            } else if($scope.scope==2){
                $scope.scopeId=$rootScope.currAssembly.id;
            } else if($scope.scope==3){
                $scope.scopeId=$rootScope.currRule.id;
            } else if($scope.scope==4){
                $scope.scopeId=$rootScope.currDecisionTable.id;
            } else if($scope.scope==5){
                $scope.scopeId=$rootScope.currDecisionTree.id;
            }
            $scope.variableParams = {
                "scope":$scope.scope,
                "scopeId":$scope.scopeId
            };
            $scope.variable={
                "scope":$scope.scope,
                "scopeId":$scope.scopeId
            };
            $http({
                url:"/grule-manager/variables",
                method:"GET",
                params:$scope.variableParams
            }).success(function(resp){
                $scope.variables=resp;
            });
        });



        $scope.update=function(row){
            GillionMsg.showUrl({
                title: '修改变量',
                url: '/html2/rule/variable/updateVariable',
                width: 400,
                height:250,
                data:row,
                onClose: function(resp){
                    if(resp.success){
                        GillionMsg.alert("提示", "变量修改成功");
                        $http({
                            url:"/grule-manager/variables",
                            method:"GET",
                            params:$scope.variableParams
                        }).success(function(resp){
                            $scope.variables=resp;
                        });
                        angular.element(document).trigger('refresh.variables');
                    }
                }
            });
        };

        $scope.add=function(){
            $scope.variable.scope= $scope.scope;
            $scope.variable.scopeId=$scope.scopeId;
            angular.element('form[name="RuleVariableForm"]').scope().RuleVariableForm.verify().then(function(){
                if($scope.variable.valueType==0&&isNaN($scope.variable.varValueExpr)==true){
                    GillionMsg.alert("提示", "请输入数字类型的表达式！");
                    return;
                }else if($scope.variable.valueType==1&&$scope.variable.varValueExpr!=undefined){
                    $scope.variable.varValueExpr="'"+ $scope.variable.varValueExpr+"'";
                }else if($scope.variable.valueType==2&&$scope.variable.varValueExpr!="true"&&$scope.variable.varValueExpr!="false"){
                    GillionMsg.alert("提示", "请输入布尔类型的表达式！");
                    return;
                }
                Resource("/grule-manager/variables/"+$scope.scope+"/"+$scope.scopeId+"/:id", {id: "@id"}).create($scope.variable,function(response){
                    if (response.success) {
                        $http({
                            url:"/grule-manager/variables",
                            method:"GET",
                            params:$scope.variableParams
                        }).success(function(resp){
                            $scope.variables=resp;
                            $scope.variable={};
                        });
                        angular.element(document).trigger('refresh.variables');
                    }
                })
            })
        };

        $scope.remove=function(row){
            GillionMsg.confirm('提示信息', '是否确定删除？', function(r) {
                if (r) {
                    Resource("/grule-manager/variables/"+$scope.scope+"/"+$scope.scopeId+"/:id", {id: "@id"}).remove(row, function (result) {
                        if (result.success) {
                            GillionMsg.alert("提示", "变量删除成功.");
                            $http({
                                url:"/grule-manager/variables",
                                method:"GET",
                                params:$scope.variableParams
                            }).success(function(resp){
                                $scope.variables=resp;
                            });
                            angular.element(document).trigger('refresh.variables');
                        } else {
                            GillionMsg.alert("提示", "变量删除失败.");
                        }
                    })
                }
            })
        }

        $scope.addFromJson=function(){
            var fd =new FormData();
            var file = document.getElementById('variableFile').files[0];
            if(file==undefined){
                GillionMsg.alert('提示', '请选择文件！');
                return;
            }

            fd.append('file', file);
            fd.append('scope',$scope.scope);
            fd.append('scopeId',$scope.scopeId);
            $http({
                method:'POST',
                url:"/grule-manager/variables/addFromJson",
                data: fd,
                headers: {'Content-Type':undefined},
                transformRequest: angular.identity
            }).success( function ( response ) {
                if(response.success){
                    GillionMsg.alert('提示', '导入成功！');
                    refresh();
                    $scope.detailsShow = true;
                }
            });
        }

    }

});